package com.talent.controller;

import com.talent.common.Result;
import com.talent.pojo.dto.CategoryDTO;
import com.talent.pojo.entity.Category;
import com.talent.service.CategoryService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类控制器
 */
@RestController
@RequestMapping("/api/categories")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据类型查询分类列表
     */
    @GetMapping
    public Result<List<Category>> getByType(@RequestParam(required = false) String type) {
        log.info("根据类型查询分类列表：{}", type);
        List<Category> categories = categoryService.getByType(type);
        return Result.success(categories);
    }

    /**
     * 根据ID查询分类
     */
    @GetMapping("/{id}")
    public Result<Category> getById(@PathVariable Long id) {
        log.info("根据ID查询分类：{}", id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 新增分类
     */
    @PostMapping
    public Result<String> save(@Valid @RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success("新增成功");
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public Result<String> update(@PathVariable Long id, @Valid @RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类：id={}, data={}", id, categoryDTO);
        categoryService.update(id, categoryDTO);
        return Result.success("更新成功");
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success("删除成功");
    }
}
